package com.jfcloud.mall.storage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jfcloud.common.http.RespDataT;
import com.jfcloud.mall.storage.domain.JfcloudStorage;
import com.jfcloud.mall.storage.domain.dto.StorageDeductForm;
import com.jfcloud.mall.storage.domain.dto.StorageQueryForm;
import com.jfcloud.mall.storage.persistent.JfcloudStorageService;
import com.jfcloud.mall.storage.service.StorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存服务
 * @author zhaojunfu
 * @since 2020-09-09
 */
@Api(tags = "库存API")
@Controller
@RequestMapping("/storage")
public class JfcloudStorageController {
    private static final Logger LOGGER = LoggerFactory.getLogger(JfcloudStorageController.class);

    @Autowired
    private JfcloudStorageService jfcloudStorageService;

    @Autowired
    private StorageService StorageService;

    @ApiOperation("商品库存查询")
    @GetMapping(path = "/getStorages")
    @ResponseBody
    public RespDataT<List<JfcloudStorage>> getStorages(StorageQueryForm  query) {
        try{
            List<JfcloudStorage> datas = jfcloudStorageService.list(
                    new QueryWrapper<JfcloudStorage>()
                            .in("product_id", query.getProductIds()));
            return new RespDataT<List<JfcloudStorage>>(datas);
        }catch (Exception e) {
            LOGGER.error(e.getMessage(),e);
            return new RespDataT<List<JfcloudStorage>>(e);
        }
    }
    @ApiOperation("扣减库存")
    @PostMapping(path = "/deductStorage",consumes = "application/json", produces = "application/json")
    @ResponseBody
    public RespDataT<JfcloudStorage> deductStorage(@RequestBody StorageDeductForm form) {
        try{
            JfcloudStorage storage=StorageService.deduct(form);
            return new RespDataT<JfcloudStorage>(storage);
        }catch (Exception e) {
            LOGGER.error(e.getMessage(),e);
            return new RespDataT<JfcloudStorage>(e);
        }
    }

}
